package com.ruoyi.project.hissystem.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.hissystem.domain.HisRole;
import com.ruoyi.project.hissystem.service.HisRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : 杨磊
 * @date : 15:13 2021/8/19
 */

@RestController
@RequestMapping("/hissystem/role")
public class HisRoleController extends BaseController {


    //声明业务层属性
    @Autowired
    private HisRoleService hisRoleService;


    /**
     * 分页动态查询角色信息
     * @param role
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hissystem:role:list')")
    @GetMapping("/list")
    public TableDataInfo list(HisRole role)
    {
        startPage();
        List<HisRole> list = hisRoleService.selectRoleList(role);
        return getDataTable(list);
    }

    //新增角色信息
    @PreAuthorize("@ss.hasPermi('hissystem:role:add')")
    @Log(title = "角色管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HisRole role)
    {
        role.setCreateBy(SecurityUtils.getUsername());
        return toAjax(hisRoleService.insertRole(role));

    }

    //根据角色id查询详细信息【数据回显】
    @PreAuthorize("@ss.hasPermi('hissystem:role:query')")
    @GetMapping(value = "/{roleId}")
    public AjaxResult getInfo(@PathVariable Long roleId)
    {
        return AjaxResult.success(hisRoleService.selectRoleById(roleId));
    }

    //修改角色信息
    /**
     * 修改保存角色
     */
    @PreAuthorize("@ss.hasPermi('hissystem:role:edit')")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HisRole role) {

        if (hisRoleService.updateRole(role) > 0){
            return AjaxResult.success();
        }
        return AjaxResult.error("修改角色'" + role.getRoleName() + "'失败，请联系管理员");
    }

    //删除角色
    @PreAuthorize("@ss.hasPermi('hissystem:role:remove')")
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{roleIds}")
    public AjaxResult remove(@PathVariable Long[] roleIds)
    {
        return toAjax(hisRoleService.deleteRoleByIds(roleIds));
    }



}
